<template>
  <el-dialog
    title="选择电子证照编号"
    :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    append-to-body
    width="1200px"
  >
    <el-form
      :model="searchParams"
      ref="queryForm"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="电子证照编号" prop="ecNum">
        <el-input
          v-model.trim="searchParams.ecNum"
          placeholder="请输入"
          clearable
          size="small"
          class="form-content-width"
        />
      </el-form-item>
      <el-form-item label="行政区划" prop="adCd">
        <el-select
          v-model="searchParams.adCd"
          size="small"
          filterable
          clearable
          class="form-content-width"
          placeholder="请选择"
        >
          <el-option
            v-for="item in district"
            :key="item.adCd"
            :label="item.adNm"
            :value="item.adCd"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位名称" prop="watRigOwner">
        <el-input
          v-model.trim="searchParams.watRigOwner"
          placeholder="请输入"
          clearable
          size="small"
          class="form-content-width"
        />
      </el-form-item>
      <el-form-item label="发证机关单位" prop="orgCd">
        <el-select
          v-model="searchParams.issuAuthCode"
          size="small"
          filterable
          clearable
          class="form-content-width"
          placeholder="请选择"
        >
          <el-option
            v-for="item in issuAuthOptions"
            :key="item.orgCd"
            :label="item.orgNm"
            :value="item.orgCd"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>

    <div class="table-item">
      <el-table
        ref="multipleTable"
        v-loading="tableLoading"
        height="36vh"
        :data="tableData"
        @selection-change="handleSelectionChange"
        stripe
        border
      >
        <el-table-column type="selection" width="46" align="center" />
        <el-table-column label="序号" align="center" type="index" width="60" />
        <el-table-column
          label="电子证照编号"
          align="center"
          prop="ecNum"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="取水单位名称"
          align="center"
          prop="watRigOwner"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="统一社会信用代码"
          align="center"
          prop="ownerCode"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="行政区划"
          align="center"
          prop="adNm"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column
          label="许可水量（万m³）"
          align="center"
          prop="watTAmnt"
          min-width="140"
          show-overflow-tooltip
        />
        <el-table-column
          label="发证机关单位"
          align="center"
          prop="issuAuth"
          min-width="160"
          show-overflow-tooltip
        />
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-item">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, prev, pager, next, sizes"
        :total="tableTotal"
      >
      </el-pagination>
    </div>

    <!-- 按钮 -->
    <div class="op-btn-box">
      <el-button @click="dialogVisible = false">关 闭</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { ecNumList } from "@/api/businessManagement/waterUseEfficiencyControl/measurementFacilitiesBasicInfo";
import { orgNmOptions } from "@/api/businessManagement/waterIntakePermitManage/waterIntakeLicenseElectronicCertification.js";
import { tableMixin } from "@/mixins/tableMixin.js";
import { districtOptionsMixin } from "@/mixins/districtOptionsMixin.js";
export default {
  name: "selectEcNum",
  mixins: [tableMixin, districtOptionsMixin],
  data() {
    return {
      dialogVisible: false,
      // 查询参数
      searchParams: {
        ecNum: "",
        adCd: "",
        watRigOwner: "",
        issuAuthCode: "",
      },
      issuAuthOptions: [],
      singleType: false,
    };
  },
  created() {
    this.getOrgList();
  },
  methods: {
    open(singleType = false) {
      this.singleType = singleType;
      console.log(this.singleType);
      this.multipleSelection = [];
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.queryForm.resetFields();
        this.handleSearch();
      });
    },
    /** 查询参数列表 */
    async search() {
      this.tableLoading = true;
      let { searchParams, pageSize, pageNum } = this;
      let params = { ...searchParams, pageIndex: pageNum, pageSize };

      let { status, data } = await ecNumList(params);
      if (status == 200) {
        this.tableData = data.list;
        this.tableTotal = data.total;
      } else {
        this.tableData = [];
        this.total = 0;
      }
      this.tableLoading = false;
    },
    // 获取发证机关选项
    async getOrgList() {
      let { status, data } = await orgNmOptions();
      if (status == 200) {
        this.issuAuthOptions = data;
      }
    },
    submit() {
      if (this.multipleSelection.length == 0) {
        this.msgWarning("请选择电子证照编号");
        return false;
      } else if (this.singleType && this.multipleSelection.length != 1) {
        this.msgWarning("只能选择一条电子证照编号");
        return false;
      } else if (this.multipleSelection.length != 1) {
        this.msgWarning("只能选择一条电子证照编号");
        return false;
      }
      this.$emit("update-ecnum-data", [...this.multipleSelection]);
      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/styles/water-save-form.scss";
.form-content-width {
  width: 160px;
}
.op-btn-box {
  margin-top: 16px;
}
.relation-btn {
  color: #409eff;
  cursor: pointer;
  &.selected {
    color: #67c23a;
  }
}
.pagination-item {
  margin-top: 10px;
}
</style>